<?php
/**
 * 物流自提服务站父类
 *
 *
 * @网店运维提供技术支持 授权请购买shopnc授权
 * @license    http://www.shopwwi.com
 * @link       交流群号：111731672
 */



defined('ByShopWWI') or exit('Access Invalid!');

class Control{
    /**
     * 检查短消息数量
     *
     */
    protected function checkMessage() {
        if($_SESSION['member_id'] == '') return ;
        //判断cookie是否存在
        $cookie_name = 'msgnewnum'.$_SESSION['member_id'];
        if (cookie($cookie_name) != null){
            $countnum = intval(cookie($cookie_name));
        }else {
            $message_model = Model('message');
            $countnum = $message_model->countNewMessage($_SESSION['member_id']);
            setNcCookie($cookie_name,"$countnum",2*3600);//保存2小时
        }
        Tpl::output('message_num',$countnum);
    }

    /**
     *  输出头部的公用信息
     *
     */
    protected function showLayout() {
        $this->checkMessage();//短消息检查
        $this->article();//文章输出
        $this->systemmsg();//消息条数提示
        $this->showCartCount();

        //热门搜索
        Tpl::output('hot_search',@explode(',',C('hot_search')));
        if (C('rec_search') != '') {
            $rec_search_list = @unserialize(C('rec_search'));
        }
        Tpl::output('rec_search_list',is_array($rec_search_list) ? $rec_search_list : array());
        
        //历史搜索
        if (cookie('his_sh') != '') {
            $his_search_list = explode('~', cookie('his_sh'));
        }
        Tpl::output('his_search_list',is_array($his_search_list) ? $his_search_list : array());

        $model_class = Model('goods_class');
        $goods_class = $model_class->get_all_category();
        Tpl::output('show_goods_class',$goods_class);//商品分类

        //获取导航
        Tpl::output('nav_list', rkcache('nav',true));
        //查询保障服务项目
        Tpl::output('contract_list',Model('contract')->getContractItemByCache());
    }

    /**
     * 显示购物车数量
     */
    protected function showCartCount() {
        if (cookie('cart_goods_num') != null){
            $cart_num = intval(cookie('cart_goods_num'));
        }else {
            //已登录状态，存入数据库,未登录时，优先存入缓存，否则存入COOKIE
            if($_SESSION['member_id']) {
                $save_type = 'db';
            } else {
                $save_type = 'cookie';
            }
            $cart_num = Model('cart')->getCartNum($save_type,array('buyer_id'=>$_SESSION['member_id']));//查询购物车商品种类
        }
        Tpl::output('cart_goods_num',$cart_num);
    }
    
    /**
     * 系统公告
     */
    protected function system_notice() {
        $model_message  = Model('article');
        $condition = array();
        $condition['ac_id'] = 1;
        $condition['article_position_in'] = ARTICLE_POSIT_ALL.','.ARTICLE_POSIT_BUYER;
        $condition['limit'] = 5;
        $article_list  = $model_message->getArticleList($condition);
        Tpl::output('system_notice',$article_list);
    }

    /**
     * 输出会员等级
     * @param bool $is_return 是否返回会员信息，返回为true，输出会员信息为false
     */
    protected function getMemberAndGradeInfo($is_return = false){
        $member_info = array();
        //会员详情及会员级别处理
        if($_SESSION['member_id']) {
            $model_member = Model('member');
            $member_info = $model_member->getMemberInfoByID($_SESSION['member_id']);
            if ($member_info){
                $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_info['member_exppoints']));
                $member_info = array_merge($member_info,$member_gradeinfo);
                $member_info['voucher_count'] = Model('voucher')->getCurrentAvailableVoucherCount($_SESSION['member_id']);
                $member_info['redpacket_count'] = Model('redpacket')->getCurrentAvailableRedpacketCount($_SESSION['member_id']);
                $member_info['security_level'] = $model_member->getMemberSecurityLevel($member_info);
            }
        }
        if ($is_return == true){//返回会员信息
            return $member_info;
        } else {//输出会员信息
            Tpl::output('member_info',$member_info);
        }
    }

    /**
     * 验证会员是否登录
     *
     */
    protected function checkLogin(){
        if ($_SESSION['is_login'] !== '1'){
            $ref_url = request_uri();
            if ($_GET['inajax']){
                showDialog('','','js',"login_dialog();",200);
            }else {
                @header("location: " . urlLogin('login', 'index', array('ref_url' => $ref_url)));
            }
            exit;
        }
    }

    //消息条数提示
    public function systemmsg(){
        $msg_count= array();
        $model_message	= Model('message');
        $message_array	= $model_message->listMessage(array('from_member_id'=>'0','message_type'=>'1','to_member_id'=>$_SESSION['member_id'],'no_del_member_id'=>$_SESSION['member_id']),$page);
        if (!empty($message_array) && is_array($message_array)){
            foreach ($message_array as $k=>$v){
                $v['message_open'] = '0';
                if (!empty($v['read_member_id'])){
                    $tmp_readid_arr = explode(',',$v['read_member_id']);
                    if (in_array($_SESSION['member_id'],$tmp_readid_arr)){
                        $v['message_open'] = '1';
                    }
                }
                $v['from_member_name'] = Language::get('home_message_system_message');
                $message_array[$k]	= $v;
            }
        }

        if(!empty($message_array)){
        foreach ($message_array as $_value){
            if($_value['message_open'] == '0'){
                array_push($msg_count, $_value);
            }
        }
    }

        Tpl::output('msg_count', count($msg_count));
    }

    //文章输出
    protected function article() {

        $article_cms=  Model('cms_article');
        $article_class=  Model('cms_article_class');

        //账户与资产
        $account_class_id= $article_cms->article_select('account');
        //付款及退款
        $pay_class_id= $article_cms->article_select('pay');
        //售后规则
        $sale_class_id= $article_cms->article_select('sale');
        //购买咨询
        $consult_class_id= $article_cms->article_select('consult');
        //获利规则
        $profit_class_id= $article_cms->article_select('profit');

        //底部菜单导航
        $nav_footer=$article_cms->nav_footer();

        $class_list = array();
        $condition= array();
        $condition['class_id']=array('in', "$account_class_id,$pay_class_id,$sale_class_id,$consult_class_id,$profit_class_id");
        $data['article_state'] = 3;
        $data['foot'] = 1;
        $data['article_class_id']= $condition['class_id'];
        $article_c= $article_class->getList($condition,$page=null,$order='class_sort asc',$field='*');
        $article_detail= $article_cms->getList($data, $page=null, $order='article_sort asc', $field='article_id,
        article_title,
        article_class_id',
            $limit='100');

        foreach ($article_c as $_key=>$_value){
            $class_list[$_key]=$_value;
        }

        foreach ($class_list as $_k => $_v){
            $i=0; $count=3;
            foreach ($article_detail as $_key=>$_value){

                if ($_v['class_id'] == $_value['article_class_id']){
                    $i++;
                    $class_list[$_k]['list'][$_key] = $_value;
                    if($i>$count) break;
                }
            }
        }

        Tpl::output('nav_footer',  $nav_footer);
        Tpl::output('article_list',  $class_list);
    }
    
    /**
     * 自动登录
     */ 
    protected function auto_login() {
        $data = cookie('auto_login');
        if (empty($data)) {
            return false;
        }
        $model_member = Model('member');
        if ($_SESSION['is_login']) {
            $model_member->auto_login();
        }
        $member_id = intval(decrypt($data, MD5_KEY));
        if ($member_id <= 0) {
            return false;
        }
        $member_info = $model_member->getMemberInfoByID($member_id);
        $model_member->createSession($member_info);
    }
}
class BaseLoginControl extends Control{
    /**
     * 构造函数
     */
    public function __construct(){
        /**
         * 读取通用、布局的语言包
         */
        Language::read('common,core_lang_index');
        /**
         * 设置布局文件内容
         */
        Tpl::setLayout('login_layout');

        /**
         * 获取导航
         */
        Tpl::output('nav_list', rkcache('nav',true));
        
        /**
         * 自动登录
         */
        $this->auto_login();
    }

}

class BaseMemberControl extends Control {
    protected $member_info = array();   // 会员信息
    public function __construct(){
        if(!C('site_status')) halt(C('closed_reason'));

        Language::read('common,member_layout');

        //会员验证
        $this->checkLogin();
        //输出头部的公用信息
        $this->showLayout();
        Tpl::setLayout('member_layout');

        //获得会员信息
        $this->member_info = $this->getMemberAndGradeInfo(true);
        Tpl::output('member_info', $this->member_info);

        // 左侧导航
        $menu_list = $this->_getMenuList();
        Tpl::output('menu_list', $menu_list);

        // 系统消息
        $this->system_notice();
        // 页面高亮
        Tpl::output('act', $_GET['act']);
        /**
         * 文章
         */
        $this->article();
    }
    
    /**
     * 左侧导航
     * 菜单数组中child的下标要和其链接的act对应。否则面包屑不能正常显示
     * @return array
     */
    private function _getMenuList() {
        $menu_list = array(
            'info' => array('name' => '会员资料', 'child' => array(
                'member_information'=> array('name' => '账户信息', 'url'=>urlMember('member_information', 'member')),
                'member_security'   => array('name' => '账户安全', 'url'=>urlMember('member_security', 'index')),
                'member_address'    => array('name' => '收货地址', 'url'=>urlMember('member_address', 'address')),
                'member_message'    => array('name' => '我的消息', 'url'=>urlMember('member_message', 'systemmsg')),
            )),
            'property' => array('name' => '财产中心', 'child' => array(
                'consume'           => array('name' => '消费记录', 'url'=>urlMember('consume')),
                'predeposit'        => array('name' => '账户余额', 'url'=>urlMember('predeposit', 'pd_log_list')),
                'member_points'     => array('name' => '我的积分', 'url'=>urlMember('member_points', 'index')),
                'member_voucher'    => array('name' => '我的优惠券', 'url'=>urlMember('member_voucher', 'index')),
            ))
        );
        return $menu_list;
    }
}
class BaseArticleControl extends Control{
    /**
     * 构造函数
     */
    public function __construct(){
        /**
         * 读取通用、布局的语言包
         */
        Language::read('common,core_lang_index');
        /**
         * 设置布局文件内容
        */
        Tpl::setLayout('article_layout');

        /**
         * 获取导航
        */
        Tpl::output('nav_list', rkcache('nav',true));

        /**
         *  输出头部的公用信息
        */
        $this->showLayout();
        
        /**
         * 文章
         */
        $this->article();
    }

}
